import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.SimpleAccountRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Before;
import org.junit.Test;

public class TestShiro1 {
    SimpleAccountRealm simpleAccountRealm = new SimpleAccountRealm();
    @Before
    public void setUser(){
        simpleAccountRealm.addAccount("admin","123456","user:update");
    }
    @Test
    public void  testShiro(){
        //1.构建管理器环境
        DefaultSecurityManager securityManager = new DefaultSecurityManager();
        //将用户加入进去
        securityManager.setRealm(simpleAccountRealm);
        //2.提交认证请求
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        //认证
        UsernamePasswordToken token = new UsernamePasswordToken("admin","123456");
        subject.login(token);
        System.out.println("isAuthenticated:"+subject.isAuthenticated());
        System.out.println("checkeRoles:"+subject.hasRole("user:update"));
    }
}
